#include "editplacewid.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMessageBox>
#include "Place.h"
#include "PlaceManger.h"

EditPlaceWid::EditPlaceWid(QWidget *parent) : QWidget(parent)
{
    this->setWindowTitle("景点信息修改");
    resize(500,300);
    QHBoxLayout *nameLayout = new QHBoxLayout();
    QHBoxLayout *descLayout = new QHBoxLayout();
    QHBoxLayout *btnLayout = new QHBoxLayout();
    QVBoxLayout *vlayout = new QVBoxLayout(this);

    this->oldName = new QLabel(this);
    this->nameLab = new QLabel("新的名称：",this);
    this->descLab = new QLabel("景点介绍：",this);
    this->nameEdit = new QLineEdit(this);
    nameEdit->setPlaceholderText("10个字以内");
    this->descEdit = new QTextEdit(this);
    descEdit->setPlaceholderText("500个字以内");
    this->confirmBtn = new QPushButton("保存",this);
    this->confirmBtn->setMaximumWidth(200);
    nameLayout->addWidget(nameLab);
    nameLayout->addWidget(nameEdit);

    descLayout->addWidget(descLab);
    descLayout->addWidget(descEdit);
    btnLayout->addWidget(confirmBtn);

    vlayout->addWidget(this->oldName);
    vlayout->addLayout(nameLayout);
    vlayout->addLayout(descLayout);
    vlayout->addLayout(btnLayout);
    vlayout->setSpacing(20);
    vlayout->setMargin(40);
    connect(this->confirmBtn,&QPushButton::released,this,&EditPlaceWid::SaveData);
}

void EditPlaceWid::SaveData()
{
    std::string name;
    std::string desc;
    if(this->nameEdit->text().length()>10)
    {
        QMessageBox::information(this,"warning","你输入的地名过长!",QMessageBox::Yes);
        return ;
    }
    name = (char *)this->nameEdit->text().toStdString().c_str();
    desc = (char *)this->descEdit->toPlainText().toStdString().c_str();
    Place place(1,(char *)name.c_str(),(char *)desc.c_str());

    int res = QMessageBox::information(this,"ok","您确定要修改吗？",QMessageBox::Yes,QMessageBox::No);
    if(res==QMessageBox::Yes)
    {
        PlaceManger PM;
        PM.Edit(this->editName,place);
        emit dataSaved();
    }
}

void EditPlaceWid::setEditName(char *name)
{
    strcpy(this->editName,name);
    this->oldName->setText(QString("你要修改的是：")+this->editName);
}
